﻿using System;
using System.Text;
using System.Text.RegularExpressions;

namespace T.FAS.Runtime.DataAccess.CDOEngine
{
    /// <summary>
    /// 过滤条件语句类
    /// </summary>
    /// <remarks>过滤条件语句类</remarks>
    public class FilterConditionStatement : SqlElement
    {
        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public FilterConditionStatement()
            : base()
        {
            base.CreateChildCollection();
        }

        #endregion

        #region 属性
        public QueryGroupLogicalOperator QueryGroupLogicalOperator { get; set; }
        #endregion

        #region 方法

        /// <summary>
        /// 转换成SQL语句
        /// </summary>
        /// <returns>SQL语句</returns>
        public override string ToSQL()
        {
            if (ChildCollection.Count == 0)
                return string.Empty;

            var condition = new StringBuilder(ChildCollection.Count * 56);
            for (int i = 0; i < ChildCollection.Count; i++)
            {
                //判断前面的SQL是否为空，是则拼接and or连接符
                bool isFirstClause = string.IsNullOrEmpty(condition.ToString().Trim());
                if (ChildCollection[i] == null) continue;
                if (ChildCollection[i] is ConditionStatement subCondition)
                {
                    if (subCondition == null) continue;
                    if (i == 0)
                    {
                        condition.Append(subCondition.ToSQL());
                    }
                    else
                    {
                        if (subCondition.LogicalOperator == OperatorType.And)
                        {
                            condition.Append(And.Clause(subCondition.ToSQL()));
                        }
                        else
                        {
                            condition.Append(Or.Clause(subCondition.ToSQL()));
                        }
                    }
                }
                else if(ChildCollection[i] is FilterConditionSpecifySubClassStament filterConditionSpecifySubClassStament) //处理exists
                {
                    var existsSQL = filterConditionSpecifySubClassStament.ToSQL();
                    if (isFirstClause)
                    {
                        Regex regex = new Regex("^\\s+((and|or))", RegexOptions.IgnoreCase);
                        existsSQL = regex.Replace(existsSQL, "");
                    }
                    condition.Append(existsSQL);
                }
                else if (ChildCollection[i] is FilterConditionStatement filterConditionStatement) //添加括号
                {
                    var sql = filterConditionStatement.ToSQL();
                    if (!string.IsNullOrEmpty(sql))
                    {
                        if (!isFirstClause)
                        {
                            condition.Append(" ");
                            condition.Append(filterConditionStatement.QueryGroupLogicalOperator.ToString());
                        }
                        condition.Append("(");
                        condition.Append(sql);
                        condition.Append(")");
                    }
                }
                else if (ChildCollection[i] is SqlPrimaryKey sqlPrimaryKey) //添加SqlPrimaryKey主键条件
                {
                    condition.Append(sqlPrimaryKey.ToSQL());
                }
            }

            return condition.ToString();
        }


        /// <summary>
        /// 转换成SQL语句
        /// </summary>
        /// <returns>SQL语句</returns>
        public string ToSQL_backup()
        {
            if (ChildCollection.Count == 0)
                return string.Empty;

            var condition = new StringBuilder(ChildCollection.Count * 56);
            for (int i = 0; i < ChildCollection.Count; i++)
            {
                if (ChildCollection[i] == null) continue;
                if (ChildCollection[i] is ConditionStatement)
                {
                    var subCondition = ChildCollection[i] as ConditionStatement;
                    if (subCondition == null) continue;
                    if (i == 0)
                    {
                        condition.Append(subCondition.ToSQL());
                    }
                    else
                    {
                        if (subCondition.LogicalOperator == OperatorType.And)
                        {
                            condition.Append(And.Clause(subCondition.ToSQL()));
                        }
                        else
                        {
                            condition.Append(Or.Clause(subCondition.ToSQL()));
                        }
                    }
                }
                else  //SqlPrimaryKey
                {
                    condition.Append(ChildCollection[i].ToSQL());
                }
            }

            return condition.ToString();
        }

        #endregion

    }

    /// <summary>
    /// 逻辑运算符
    /// </summary>
    public enum QueryGroupLogicalOperator
    {
        AND = 0,
        OR,
        //None,
    }
}
